<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
use app\admin\model\Article as ArticleModel;
class Article extends Base
{
    public function index_cate()
    {
        $createTime = 'create_time';
        $updateTime = 'update_time';
        $cateObj = Db::name('cate')->select();
        $this->assign('cateObj',$cateObj);
        //dump($cateObj);
    	return view();
    }
    public function add_cate()
    {
      $cate = Db::name('cate')->select();
    	if(request()->IsAjax()){
	    	$shujv = input('post.');
	    	$shujv['create_time'] = time();
	    	$shujv['update_time'] = time();
	    	$result = Db::name('cate')->update($shujv);
	    	if($result===false){
	    		exit(json_encode(array('status'=>'n','info'=>'数据添加失败!')));
	    	}else{
	    		exit(json_encode(array('status'=>'y','info'=>'数据添加成功!')));
	    	}
    	}
        $this->assign('cate',$cate);
        return view();
    }
    public function sort()
    {
      if(request()->IsAjax()){
          $param = input('post.');     
          $cate = Db::name('cate');
          foreach ($param as $id => $sort){
              $cate->where(array('cate_id' => $id ))->setField('sort' , $sort);
              //dump($cate);
          }
          if($sort===false){
               exit(json_encode(array('status'=>'n','info'=>'数据更新失败!')));
          }else{
               exit(json_encode(array('status'=>'y','info'=>'数据更新成功!')));
          }
       }
    }
    public function edit_cate()
    {
        if(request()->IsAjax()) {
            $id = input('post.cate_id');
            $shujv = input('post.');
            $shujv['update_time'] = time();
            $result = Db::name('cate')->where(array('cate_id' => $id))->update($shujv);
            if ($result === false) {
                exit(json_encode(array('status' => 'n', 'info' => '数据修改失败!')));
            } else {
                exit(json_encode(array('status' => 'y', 'info' => '数据修改成功!')));
            }
        }else{
            $cate_id = input('cate_id');
            $cate = Db::name('cate')->where(array('cate_id'=>$cate_id))->find();
        }
        $this->assign('cate',$cate);
        return view();

    }
    public function del_cate()
    {
        $id = input('post.id');
        $result = Db::name('cate')->where(array('cate_id'=>$id))->delete($id);
    }
    public function index_article()
    {
//        $artObj = ArticleModel::paginate(10);
//        $artObj = ArticleModel::paginate(10);
        $artObj = ArticleModel::order('update_time desc')->paginate(10);
        $this->assign('artObj',$artObj);
        return view();
    }
    public function  add_article()
    {
        if(request()->isAjax()){
            $shujv = input('post.');
            $shujv['content'] = input('post.content');
            $shujv['create_time'] = time();
            $shujv['update_time'] = time();
            $result = Db::name('article')->insert($shujv);
            if($result===false){
                return json_encode(array('status'=>'n','info'=>'文章添加失败!'));
            }else{
                return json_encode(array('status'=>'n','info'=>'文章添加成功!'));
            }
        }
        $cate = Db::name('cate')->select();
        $this->assign('cate',$cate);
        return view();
    }
    public function edit_article()
    {
        if(request()->IsAjax()) {
            $id = input('post.id');
            $shujv = input('post.');

            $shujv['update_time'] = time();//dump($shujv);exit;
            $result = Db::name('article')->where(array('id' => $id))->update($shujv);
            dump($shujv);exit;
            if ($result === false) {
                exit(json_encode(array('status' => 'n', 'info' => '数据修改失败!')));
            } else {
                exit(json_encode(array('status' => 'y', 'info' => '数据修改成功!')));
            }
        }else{
            $art_id = input('id');
            $cate = Db::name('cate')->field('name,cate_id')->select();

            $article = Db::name('article')->where(array('id'=>$art_id))->find();
        }
        $this->assign('cate',$cate);
        $this->assign('article',$article);
        return view();
    }
    public function  del_article(){
        $id = input('post.id');
        $result = Db::name('article')->where(array('id'=>$id))->delete($id);
    }
}